package ltd.sshine.bot.Event;


import ltd.sshine.bot.DTO.User;
import ltd.sshine.bot.config.annotation.EventHandler;
import ltd.sshine.bot.config.annotation.EventMapping;
import ltd.sshine.bot.stroge.UserUtil;
import net.mamoe.mirai.event.events.MessageEvent;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.SingleMessage;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@EventHandler
public class UserEvent {
    RestTemplate restTemplate=new RestTemplate();
    @EventMapping("菜单")
    public String getHelp(){
        return "功能如下：\n签到\n签退\n绑定：绑定+空格+学号\n举报 ：举报 +空格+ 学号 或者 举报 + @举报的人";
    }
    @EventMapping("help")
    public String getMenu(){
        return getHelp();
    }

    @EventMapping("签到")
    public String signIn(MessageEvent event){
        Long user = UserUtil.getUser(event.getSender().getId());
        if (user!=null){
            ResponseEntity<Object> rankDTOResponseEntity;
                rankDTOResponseEntity = restTemplate.postForEntity("https://at.kexie.space/api/user/signIn",new ltd.sshine.bot.DTO.User().setUserId(user) ,Object.class);
                Map<String,Object> body = (Map<String, Object>) rankDTOResponseEntity.getBody();
                Object code = body.get("code");
                if (0== (int) code){
                    Map<String,Object> data= (Map<String, Object>) body.get("data");
                    SimpleDateFormat df = new SimpleDateFormat("HH:mm");
                    return "签到成功\n开始时间:"+df.format(new Date());
                }else{
                    return body.get("msg").toString();
                }
        }else{
            return"签到失败，请发送： 绑定+空格+学号";
        }
    }

    @EventMapping("签退")
    public String signOut(MessageEvent event)
    {
        Long user = UserUtil.getUser(event.getSender().getId());
        if (user!=null){
            ResponseEntity<Object> rankDTOResponseEntity;
            rankDTOResponseEntity = restTemplate.postForEntity("https://at.kexie.space/api/user/signOut",new User().setUserId(user) ,Object.class);
                Map<String,Object> body = (Map<String, Object>) rankDTOResponseEntity.getBody();
                Object code = body.get("code");
                if (0== (int) code){
                    Map<String,Object> data= (Map<String, Object>) body.get("data");
                    return "签退成功\n本次打卡时长:"+data.get("accumulatedTime")+"h\n本周总时长:"+data.get("totalTime")+"h";
                }else{
                    return body.get("msg").toString();
                }
        }else{
            return "签到失败，请发送： 绑定+空格+学号";
        }
    }

    @EventMapping("绑定")
    public String bind(String arg,MessageEvent event){
        if (arg==null){
            return "请发送： 绑定+空格+学号";
        }
        try {
            Long userId = Long.valueOf(arg.trim());
            UserUtil.SerUser(event.getSender().getId(),userId);
            return "绑定<"+userId+">成功";
        }catch (Exception e){
            return "绑定<"+arg+">失败，请检查格式是否正确";
        }
    }

    @EventMapping("举报")
   public String complate(String arg,MessageChain message,MessageEvent event){

        Long user = UserUtil.getUser(event.getSender().getId());

        if (message.size()==3){
            if (user==null){
                return "未绑定，请发送： 绑定 + 学号";
            }
            SingleMessage singleMessage1 = message.get(2);
            if ( singleMessage1 instanceof At){
                Long complateUser=UserUtil.getUser(((At)singleMessage1).getTarget());
                if (complateUser==null){
                    return  "被举报人未绑定学号";
                }else{
                    return complateUser( user, complateUser);
                }
            }
        }else if (message.size()==2){
            if (arg==null){
                return  "举报请发送： \\n举报 + 学号 \\n或者 \\n举报 + @举报的人";
            }
            try {
                if (user==null){
                    return "未绑定，请发送： 绑定 + 学号";
                }
                Long complateUser = Long.valueOf(arg.trim());
                return complateUser(user,complateUser);
            }catch (Exception e){
                return arg+"不正确";
            }
        }
        return  "举报请发送： \\n举报 + 学号 \\n或者 \\n举报 + @举报的人";
    }
    String complateUser(Long user,Long complateUser){
        ResponseEntity<Object> rankDTOResponseEntity;
        try{
            Map<String,Long> map=new HashMap<>();
            map.put("targetUserId",complateUser);
            map.put("operatorUserId",user);
            rankDTOResponseEntity= restTemplate.postForEntity("https://at.kexie.space/api/user/complaint",map, Object.class);
        }catch (HttpServerErrorException e){
            e.printStackTrace();
            return "服务器错误";
        }
        Map<String,Object> body = (Map<String, Object>) rankDTOResponseEntity.getBody();
        return String.valueOf(body.get("msg"));
    }
}
